package org.example.memora.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.memora.common.result.PageResult;
import org.example.memora.common.result.Result;
import org.example.memora.entity.HashTag;
import org.example.memora.service.HashTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 丁利军
 * @date 2025年06月08日 2:40
 * @description TODO: 描述
 */

@RestController
@RequestMapping("/api/hashtags")
@Tag(name = "Hashtag管理", description = "提供Hashtag标签的增查改查功能")
public class HashtagController {

    @Autowired
    private HashTagService hashtagService;

    @Operation(
            summary = "根据ID查询Hashtag",
            description = "传入Hashtag ID，返回对应的标签信息"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到标签"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/{id}")
    public Result<HashTag> getHashtagById(@PathVariable Integer id) {
        HashTag tag = hashtagService.getById(id);
        if (tag == null) {
            return Result.error("Hashtag not found");
        }
        return Result.success(tag);
    }

    @Operation(
            summary = "创建新的Hashtag",
            description = "根据传入的名称创建新的Hashtag，若已存在同名标签则返回失败"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "创建成功"),
            @ApiResponse(responseCode = "400", description = "标签已存在或创建失败"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping("/create")
    public Result<?> createHashtag(@RequestParam String name) {
        HashTag hashtag = hashtagService.createHashtag(name);

        return Result.success(hashtag);
    }

    @Operation(
            summary = "模糊搜索Hashtag",
            description = "根据关键字模糊匹配Hashtag名称，如果未传则返回所有标签"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/search")
    public Result<List<HashTag>> searchHashtags(@RequestParam(required = false) String keyword) {
        if (keyword == null || keyword.trim().isEmpty()) {
            return Result.success(hashtagService.listAll());
        }
        return Result.success(hashtagService.getHashtagsByName(keyword));
    }

    @Operation(
            summary = "批量创建Hashtag",
            description = "传入多个标签名称，逐个创建（重复的会跳过）"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "处理完成"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping("/batch-create")
    public Result<?> batchCreate(@RequestBody List<String> names) {
        hashtagService.createHashtags(names);
        return Result.success();
    }

    @Operation(
            summary = "更新Hashtag",
            description = "根据ID更新Hashtag名称"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "404", description = "标签不存在"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping("/update")
    public Result<?> update(@RequestBody HashTag hashtag) {
        boolean success = hashtagService.updateHashtag(hashtag);
        if (!success) {
            return Result.error("更新失败或标签不存在");
        }
        return Result.success();
    }
    @Operation(
            summary = "分页获取Hashtag列表",
            description = "分页返回所有的Hashtag标签"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/page")
    public Result<PageResult> getHashtagsByPage(@RequestParam(defaultValue = "1") int page,
                                                @RequestParam(defaultValue = "10") int size) {
        PageResult result = hashtagService.listHashtagsByPage(page, size);
        return Result.success(result);
    }

}

